<?php
/**
 * AccountController class file
 * ----------------------------
 * All activities that user can perform on thier account + access to panel
 *
 * LICENSE: BSD
 * @copyright  2009 Gemisoft
 * @author Deepak Pradhan <deepak.pradhan@gemisoft.com>
 * @link http://grbac.gemisoft.com/
 * @version $Id$
 * @since 0.1
 */
class AccountController extends BaseController {
/**
 * User authentication
 * 1. my account - dashboard + panel for the user
 * 2. passsord reset
 **/
  public $defaultAction='MyAccount';

  /**
   * Both layouts can be changed!
   * - grbac layout is the layout of you site to blend in the grbac module
   * - grbac_admin layout fulladmin layout with no header or footer
   */
  public function init() {
    if ((isset(Yii::app()->user->Admin))
        ||(isset(Yii::app()->user->Administrator))
        ||(isset(Yii::app()->user->Authority))) {
//      $this->layout="grbac_admin";
    } else {
//      $this->layout="grbac";
    }
  }

  /**
   * Displays the my account page to change password user info etc.
   */
  public function actionMyAccount() {
    //if (isset(Yii::app()->user->Admin)) //$this->layout="grbac_admin";
    $this->render('my_account');
  }

  /**
   * Change user password
   * @param passwordNew string
   * @param paswwordNew_repeat string
   */
  public function actionPassword_change() {
    $user = Users::model()->findbyPk(Yii::app()->user->id);
    $user->scenario='password_change';
    if(isset($_POST['Users'])) {
      $user->setAttributes($_POST['Users'], 'password_change');
      if ($user->validate()) {

      //get the slat used
        $salt = date("YmdH",strtotime($user->created));

        // make encryption of Old Password to compare with the one in table
        $password_old_crypted = $this->grbac_crypt($user->algo,$_POST['Users']['passwordOld'],$salt);

        //comapre passwordOld withe the one in the DB
        if ($user->password != $password_old_crypted) {
          $user->addError('passwordOld','Old password is incorrect');
        }

        if ($_POST['Users']['passwordNew'] != $_POST['Users']['passwordNew_repeat']) {
          $user->addError('passwordNew','New password and Password repeat do not match');
        } else {
          $user->password = $this->grbac_crypt($user->algo,$_POST['Users']['passwordNew'],$salt);
          if ($user->save()) {
            $this->render('password_changed',array('model'=>$user));
            return;
          }
        }
      }
    }
    $this->render('password_change',array('model'=>$user));
  }

  public function actionUpdate_information() {
    $id = isset($_GET['id']) ? $_GET['id'] : Yii::app()->user->id;

    $user= Users::model()->findbyPk($id);
    $user->scenario='update';

    if(isset($_POST['Users'])) {
      $user->setAttributes($_POST['Users'], 'update');
      if ($user->validate()) {
        if ($user->save()) $this->redirect(Yii::app()->user->returnUrl);
      }
    }
    $this->render('update_info',array('model'=>$user));
  }

  public function actionDeleteAccount() {
  //
  }
}
